package com.faith.dynamic.controller;

import com.faith.dynamic.bean.BaseConstant;
import com.faith.dynamic.bean.BaseController;
import com.faith.dynamic.bean.BaseRequestObj;
import com.faith.dynamic.bean.BaseResponseObj;
import com.faith.dynamic.model.AddForDynamicDto;
import com.faith.dynamic.model.CancelForDynamicDto;
import com.faith.dynamic.model.UpdateForDynamicDto;
import com.faith.dynamic.service.DynamicScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.validation.Valid;

/**
 * @ClassName DynamicScheduleController$
 * @Description 动态定时任务控制器
 * @Author faith$
 * @Date 2019/8/12 10:20
 * @Version 1.0
 **/
@RestController
@EnableSwagger2
@Api(description = "动态定时任务控制器")
@RequestMapping("/dynamic/schedule")
public class DynamicScheduleController extends BaseController {

    private final DynamicScheduleService dynamicScheduleService;
    @Autowired
    public DynamicScheduleController(DynamicScheduleService dynamicScheduleService){
        this.dynamicScheduleService = dynamicScheduleService;
    };
    @ApiImplicitParam(paramType = "header",name = BaseConstant.TOKEN_HEADER_NAME,value = "请求头",required = true)
    @ApiOperation(value = "添加任务",notes = "添加任务",consumes = "application/json")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponseObj add(@RequestBody @Valid @ApiParam("请求参数") BaseRequestObj<AddForDynamicDto> requestObject){
        dynamicScheduleService.add(requestObject.getData());
        return success();
    }
    @ApiImplicitParam(paramType = "header",name = BaseConstant.TOKEN_HEADER_NAME,value = "请求头",required = true)
    @ApiOperation(value = "任务列表",notes = "任务列表",consumes = "application/json")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public BaseResponseObj list(@RequestBody @Valid @ApiParam("请求参数") BaseRequestObj requestObject){
        return success(dynamicScheduleService.list());
    }
    @ApiImplicitParam(paramType = "header",name = BaseConstant.TOKEN_HEADER_NAME,value = "请求头",required = true)
    @ApiOperation(value = "更新任务",notes = "更新任务",consumes = "application/json")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public BaseResponseObj update(@RequestBody @Valid @ApiParam("请求参数") BaseRequestObj<UpdateForDynamicDto> requestObject){
        dynamicScheduleService.update(requestObject.getData());
        return success();
    }
    @ApiImplicitParam(paramType = "header",name = BaseConstant.TOKEN_HEADER_NAME,value = "请求头",required = true)
    @ApiOperation(value = "取消任务",notes = "取消任务",consumes = "application/json")
    @RequestMapping(value = "/cancel",method = RequestMethod.POST)
    public BaseResponseObj cancel(@RequestBody @Valid @ApiParam("请求参数") BaseRequestObj<CancelForDynamicDto> requestObject){
        dynamicScheduleService.cancel(requestObject.getData());
        return success();
    }
}
